var appPath = getAppPath();	
var className;
$(function(){
	var goodsClass=$("#goodsClass").val();
	if(goodsClass == "1"){
		className = "景区";
	}else if(goodsClass == "2"){
		className = "酒店";
	}else if(goodsClass == "3"){
		className = "美食";
	}else if(goodsClass == "4"){
		className = "购物";
	}else if(goodsClass == "5"){
		className = "娱乐";
	}
	
	//initJspCompont();
	initJspGrid();

	
});

function initJspCompont(){
	$("#selctMemberType").combobox({
		url:appPath+'/system/code_getCodesForCombox.do?codeTypeId=1003',
		valueField:'id',
		textField:'text'
	});
}

function initJspGrid(){
	$("#goodsGrid").datagrid({
		url:appPath+'/goods/goodsList.action',
		height:'full',
		striped: true,
		fitColumns:true,
		queryParams: getQueryParam(),
		singleSelect:false,
		checkbox:false,
		frozenColumns:[[
		      { field: 'ck',checkbox:'true',width:10,align:'center',sortable:false},
		      /*{ field: 'ck',title:'',width:10,align:'center',
		    	  formatter: function(value, rowData, rowIndex){
		                return '<input type="radio" name="selectRadio" id="selectRadio"' + rowIndex + '    value="' + rowData.id + '" />';
		            }
		      },*/
		      {field:'op',title:'操作',width:32,align:'center',
		    	  formatter:function(value,rowData,rowIndex){
		    		  if(!rowData.id)return "";
		    			  return "<a href='javascript:void(0)'><img width='12px' height='12px' style='border:0px' src='"+appPath+"/js/lib/jquery-easyui/themes/icons/pencil.png' title='编辑' onClick='showEdit("+rowData.id+")'/></a>";
		    	  }
		      },
		      {field:'pre',title:'预览',width:32,align:'center',
		    	  formatter:function(value,rowData,rowIndex){
		    		  if(!rowData.id)return "";
		    			  return "<a href='javascript:void(0)'><img width='12px' height='12px' style='border:0px' src='"+appPath+"/js/lib/jquery-easyui/themes/icons/tip.png' title='预览' onClick='doPreview("+rowData.id+")'/></a>";
		    	  }
		      },
		      {field:'mappre',title:'地图预览',width:54,align:'center',
		    	  formatter:function(value,rowData,rowIndex){
		    		  if(!rowData.id)return "";
		    			  return "<a href='javascript:void(0)'><img width='12px' height='12px' style='border:0px' src='"+appPath+"/js/lib/jquery-easyui/themes/icons/tip.png' title='地图预览' onClick='doMapPreview("+rowData.id+")'/></a>";
		    	  }
		      }
		 ]],
		columns:[[
		      /*{field:'memberId',title:'会员ID',width:80,align:'left',sortable:true},
		      {field:'memberName',title:'会员名称',width:60,align:'left',sortable:true},
		      {field:'memberTypeName',title:'会员类型',width:70,align:'left',sortable:true},
		      {field:'memberStatusName',title:'会员状态',width:60,align:'left',sortable:true},
		      {field:'identityCard',title:'身份证号码',width:100,align:'left',sortable:true},
		      {field:'email',title:'电子邮箱',width:120,align:'center',sortable:true},
		      {field:'mobilephone',title:'手机',width:70,align:'left',sortable:true},
		      {field:'address',title:'所属地区',width:160,align:'left',sortable:true},
		      {field:'createTime',title:'创建时间',width:110,align:'left',sortable:true,formatter:fmtCreateTime},
		      {field:'hotFlag',title:'是否热门企业',width:110,align:'left',sortable:true,formatter:fmtHotFlag}*/
		      {field:'id',title:className+'ID',width:20,align:'left',sortable:true},
		      {field:'provinceName',title:'所在省',width:20,align:'left',sortable:true},
		      {field:'areaName',title:'地域名',width:20,align:'left',sortable:true},
		      {field:'name',title:className+'名',width:50,align:'left',sortable:true},
		      {field:'introduce',title:'介绍',width:90,align:'left',sortable:true},
		      {field:'worktime',title:'开放时间',width:30,align:'center',sortable:true},
		      {field:'longitude',title:'经度',width:20,align:'left',sortable:true},
		      {field:'latitude',title:'纬度',width:20,align:'left',sortable:true},
		      {field:'address',title:'地址',width:40,align:'left',sortable:true},
		      {field:'discountprice',title:'折扣票价',width:22,align:'left',sortable:true},
		      {field:'telephone',title:'联系电话',width:30,align:'left',sortable:true},
		      {field:'statename',title:'审核状态',width:22,align:'left',sortable:true}
		      ]],
		 hideColumn:['state'],
		 pagination:true,
		 rownumbers:true,
		 toolbar:[{
				text:'增加' + className,
				iconCls:'icon-add',
				handler:function(){
					showAdd();
				}
			},
			{
				text:className+'地图编辑',
				iconCls:'icon-edit',
				handler:function(){
					editMap();
				}
			},
			{
				text:className+'审核',
				iconCls:'icon-ok',
				handler:function(){
					doVerify();
				}
			},
			{
				text:'取消'+className+'审核',
				iconCls:'icon-cancel',
				handler:function(){
					cancelVerify();
				}
			},
			{
				text:className+'删除',
				iconCls:'icon-cancel',
				handler:function(){
					doDel();
				}
			}/*,{
				text:'重置密码',
				iconCls:'icon-reset',
				handler:function(){
					resetPassword();
				}
			},{
				text:'设为热门企业',
				iconCls:'icon-save',
				handler:function(){
					setHotCompany();
				}
			},{
				text:'取消热门企业',
				iconCls:'icon-undo',
				handler:function(){
					cancelHotCompany();
				}
			}*/]
	});
}

function doPreview(id){
		sFeatures = "dialogHeight:" + 700 + "px; dialogWidth:" + 367
				+ "px;  status:no; resizable:no; help:no";
//		if (left != null) {
//			sFeatures += "; dialogLeft:" + left;
//		}
//		if (top != null) {
//			sFeatures += "; dialogTop:" + top;
//		}
		return window.showModalDialog(appPath+"/webgoods/doPreview.action?id="+id, "", sFeatures);
}
function doMapPreview(id){
	sFeatures = "dialogHeight:" + 700 + "px; dialogWidth:" + 900
		+ "px; status:no; resizable:no; help:no";
	return window.showModalDialog(appPath+"/webgoods/mapPreview.action?id="+id, "", sFeatures);
}
function showDetail(id){
	var height = window.screen.height - 150;
	sFeatures = "dialogHeight:" + height + "px; dialogWidth:" + 1275
		+ "px; scroll:auto; status:no; resizable:no; help:no";
	sFeatures += "; dialogLeft:" ;
	sFeatures += "; dialogTop:" + 20;
	var url = appPath + "/webgoods/goodsDetail.action?id="+id;
	return window.showModalDialog(url, "", sFeatures);
}

function showAdd(){
	showAddInfo();
}

function editMap(){
	var selectResort=$("#goodsGrid").datagrid("getChecked");
	if(!selectResort || selectResort.length != 1){
		$.messager.alert('提示信息','请选择单个' +className+'进行编辑！','warning');
		return;
	}

	var resId = "";
	var exitFlg = false;
	$.each(selectResort, function(index, item){
		if(item.state=="1"){
			exitFlg = true;
		}
		resId = item.id;
		return false;
	});

//	if(exitFlg){
//		$.messager.alert('提示信息','该'+className+'已通过审核！','warning');
//		return;
//	}
//	else{
		var editUrl = appPath+"/gaode_map.html?resortid="+resId;
		window.open(editUrl);
//	}
}

//景区审核
function doVerify(){
	var selectResort=$("#goodsGrid").datagrid("getChecked");
	if(!selectResort || selectResort.length == 0){
		$.messager.alert('提示信息','请选择一个' +className+'！','warning');
		return;
	}
	var ids = "";
	var existsNo = false;
	$.each(selectResort, function(index, item){
		if(item.state!="0"){
			existsNo = true;
			return false;
		}
		ids = ids + item.id + ",";
	});
	if(existsNo){
		$.messager.alert('提示信息','只有未审核的'+className+'才能进行审核！','warning');
		return;
	}
	else {
		var url = appPath+"/webgoods/goodsVerify.action?verifyId="+ids;
		$.post(url,function(data){
			if(data=="ok"){
				$.messager.alert('提示信息',className+'审核成功。','info');
				doQuery();
			}
		});
	}
}

function cancelVerify(){
	var selectResort=$("#goodsGrid").datagrid("getChecked");
	if(!selectResort || selectResort.length == 0){
		$.messager.alert('提示信息','请选择一个' +className+'！','warning');
		return;
	}
	var ids = "";
	var existsNo = false;
	$.each(selectResort, function(index, item){
		if(item.state=="0"){
			existsNo = true;
			return false;
		}
		ids = ids + item.id + ",";
	});
	if(existsNo){
		$.messager.alert('提示信息','只有审核通过的'+className+'才能取消审核！','warning');
		return;
	}
	else {
		var url = appPath+"/webgoods/cancelVerify.action?verifyId="+ids;
		$.post(url,function(data){
			if(data=="ok"){
				$.messager.alert('提示信息',className+'审核已取消。','info');
				doQuery();
			}
		});
	}
}

function doDel(){
	var selectResort=$("#goodsGrid").datagrid("getChecked");
	if(!selectResort || selectResort.length == 0){
		$.messager.alert('提示信息','请选择一个' +className+'！','warning');
		return;
	}
	$.messager.confirm("确认", "是否确认删除", function (r) {  
        if (r) { 
			var ids = "";
			$.each(selectResort, function(index, item){
				ids = ids + item.id + ",";
			});
			var url = appPath+"/webgoods/delGoods.action?delIds="+ids;
			$.post(url,function(data){
				if(data=="ok"){
					$.messager.alert('提示信息',className+'删除成功。','info');
					doQuery();
				}
			});
        }
	});
}
//设为热门企业
function setHotCompany(){
	var selectMember=$("#memberGrid").datagrid("getSelected");
	if(!selectMember){
		$.messager.alert('提示信息','请选择一个企业！','warning');
		return;
	}
	if(selectMember.memberStatus!="2"||selectMember.memberType!="2"){
		$.messager.alert('提示信息','只有认证的货主会员才能设为热门企业！','warning');
		return;
	}
	var url = appPath+"/member/memberInfo_setHotCompany.do?hotFlag=1&memberId="+selectMember.memberId;
	$.post(url,function(data){
		if(data=="ok"){
			$.messager.alert('提示信息','热门企业设置成功。','info');
			doQuery();
		}
	});
}

//取消设置企业
function cancelHotCompany(){
	var selectMember=$("#memberGrid").datagrid("getSelected");
	if(!selectMember){
		$.messager.alert('提示信息','请选择一个企业！','warning');
		return;
	}
	if(selectMember.hotFlag!="1"){
		$.messager.alert('提示信息','请选择一个热门企业！','warning');
		return;
	}
	var url = appPath+"/member/memberInfo_setHotCompany.do?hotFlag=0&memberId="+selectMember.memberId;
	$.post(url,function(data){
		if(data=="ok"){
			$.messager.alert('提示信息','热门企业取消成功。','info');
			doQuery();
		}
	});
}

//重置用户密码
function resetPassword(){
	var selectMember=$("#memberGrid").datagrid("getSelected");
	if(!selectMember){
		$.messager.alert('提示信息','请选择要重置密码的会员！','warning');
		return;
	}
	var url = appPath+"/member/memberInfo_reset.do?memberId="+selectMember.memberId;
	$.post(url,function(data){
		if(data=="ok"){
			$.messager.alert('提示信息','密码重置成功，重置后的密码为123456','info');
		}
	});
}

/**
 * 展现增加页面
 */
function showAddInfo(){
	
	$("#submitType").val("save");
	$("#addBtn").show();
	$("#addFrame").attr("src",appPath + "/webgoods/newGoodsInit.action?id=" + "&goodsClass="+$("#goodsClass").val());
	$("#editFrame").attr("src","");
	$('#addDlg').window({
		title:'增加'+className,
		iconCls:'icon-add',
		modal: true,
		shadow: true,
		collapsible:false,
		minimizable:false,
		maximizable:false
	});
	
	$('#addDlg').window('move',{top:20,left:20});
	$('#addDlg').window('open');
}

/**
 * 展现编辑界面
 * @param {} rowData
 */
function showEdit(id){
	$("#submitType").val("save");
	//$("#addBtn").show();
	$("#editFrame").attr("src",appPath + "/webgoods/editGoodsInit.action?id="+ id + "&goodsClass="+$("#goodsClass").val());
	$('#editDlg').window({
		title:'编辑'+className,
		iconCls:'icon-add',
		modal: true,
		shadow: true,
		collapsible:false,
		minimizable:false,
		maximizable:false
	});
	
	$('#editDlg').window('move',{top:20,left:20});
	$('#editDlg').window('open');
	
}

/**
 * 会员信息审核通过
 */
function doPostData(){
	var jqFrom="";
	var memberType=$("#selctMemberType").combobox("getValue");
	if(memberType=="1")jqFrom =$("#personalForm");
	else	jqFrom =$("#companyForm");
	if(!jqFrom.form('validate')){
		return;
	}
	var type = $("#submitType").val();
	
	var url ="";
	if(type == "save"){
		url = appPath+'/member/memberInfo_save.do';
	}else{
		url = appPath+'/member/memberInfo_update.do';
	}
    var sendData = jqFrom.serialize();    
	
	$.post(url,sendData,function(data){
		if(data=="success"){
			$.messager.alert('提示信息','会员信息保存成功。','info');
			doQuery();
			cancelPostData();
		}else if(data=="existId"){
			$.messager.alert('提示信息','会员ID已存在，请重新输入!','warning');
		}else if(data=="existEmail"){
			$.messager.alert('提示信息','电子邮箱已存在，请重新输入!','warning');
		}else{
			$.messager.alert('提示信息','会员信息保存失败!请联系系统管理员!','error');
		}
	});
}

/**
 * 结束编辑（关闭对话框）
 */
function cancelPostData(){
	$('#addDlg').window('close');
}

/**
 * 获取请求信息
 */
function getQueryParam(){
	var queryObject = {};
	var queryId = $("#queryId").val();
	var queryName=$("#queryName").val();
	var queryState = $("#queryState").val();
	if(queryId&&queryId.length>0)queryObject.id = queryId;
	if(queryName&&queryName.length>0)queryObject.name = queryName;
	if(queryState&&queryState.length>0)queryObject.state = queryState;
	queryObject.goodsClass= $("#goodsClass").val();
	return queryObject;
}

/**
 * 查询数据
 */
function doQuery(){
	$("#goodsGrid").datagrid('unselectAll');
	$("#goodsGrid").datagrid('load',getQueryParam());
}

function fmtCreateTime(value, rowData, rowIndex){
	if(!value)return "";
	var s = new Date(value);
	return rowData.createTime=$.DateUtil.format(s,'yyyy/MM/dd hh:mm:ss');
}

function fmtHotFlag(value, rowData, rowIndex){
	if(rowData.hotFlag==1)return "是";
	return "否";
}
